//
//  SeeWuLiuViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/9.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//

#import "SeeWuLiuViewController.h"
#import "WuliuListTableViewCell.h"
@interface SeeWuLiuViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UILabel *wuliuNameLabel;
@property (nonatomic,strong) UILabel *wuliuNumLabel;
@property (nonatomic,strong) UIButton *copBtn;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger pageNum;
@end

@implementation SeeWuLiuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"查看物流";
    self.view.backgroundColor = kSubBacColor;
    [self.view addSubview:self.wuliuNameLabel];
    [self.view addSubview:self.wuliuNumLabel];
    [self.view addSubview:self.tableView];
    [self headerRefresh];
    
}
-(UILabel *)wuliuNameLabel{
    if (!_wuliuNameLabel) {
        _wuliuNameLabel = [TSWLTools allocLabel:@"物流类型  邦德物流" font:systemFont(TSScaleW(14)) textColor:kMainTxtColor frame:CGRectMake(TSScaleW(12), TSScaleW(12), TSScreenWidth - TSScaleW(24), TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
        NSString *string = _wuliuNameLabel.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        [atts setAttributes:@{NSForegroundColorAttributeName:kGreenColor} range:NSMakeRange(5, string.length - 5)];
        _wuliuNameLabel.attributedText = atts;
    }
    return _wuliuNameLabel;
}
-(UILabel *)wuliuNumLabel{
    if (!_wuliuNumLabel) {
        _wuliuNumLabel = [TSWLTools allocLabel:@"物流单号  13243243243" font:systemFont(TSScaleW(14)) textColor:kMainTxtColor frame:CGRectMake(TSScaleW(12), TSScaleW(12) + _wuliuNameLabel.bottom, TSScreenWidth - TSScaleW(24), TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
        _copBtn = [TSWLTools allocButtonTitle:@"复制" font:systemFont(TSScaleW(10)) textColor:kSubTxtColor image:nil frame:CGRectMake(TSScaleW(180), _wuliuNumLabel.top, TSScaleW(32), TSScaleW(15)) sel:@selector(copNumAction:) taget:self];
        _copBtn.backgroundColor = UIColorFromRGB(0xE8E6E7);
        _copBtn.cornerRadius = _copBtn.height/2.f;
        [self.view addSubview:_copBtn];
    }
    return _wuliuNumLabel;
}
-(void)copNumAction:(UIButton *)sender
{
    
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(TSScaleW(11), TSScaleW(80), TSScreenWidth - TSScaleW(22), TSScreenHeight -  Height_TabBar - TSScaleW(150)) style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kWhiteColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.cornerRadius = TSScaleW(10);

        if (@available(iOS 11.0, *))
        {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
        }
        else
        {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        WS(weakSelf);
        _tableView.mj_header = [CustomGifHeader headerWithRefreshingBlock:^{
                [weakSelf headerRefresh];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                [weakSelf footerRefresh];
        }];
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@""]];
    }
    return _dataArray;
}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WuliuListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WuliuListTableViewCell"];
    if (!cell) {
        cell = [[WuliuListTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"WuliuListTableViewCell"];
        
    }
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return TSScaleW(70);
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectZero];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScaleW(44))];

    return view;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return TSScaleW(44);
}

#pragma mark - 上拉、下拉

-(void)headerRefresh
{
    self.pageNum = 1;
    [self collectListReq];
}

-(void)footerRefresh
{
    [self collectListReq];
}
-(void)collectListReq
{
    
    NSDictionary *pama = @{@"logisticsname":_logisticsname,@"logisticsnum":_logisticsnum};
   
    [NetWorkTools postConJSONWithUrl:@"lookwuliu" parameters:pama success:^(id responseObject) {
        NSString *result = responseObject[@"result"];
        NSString *resultNote = responseObject[@"resultNote"];
        NSArray *array = responseObject[@"dataList"];
        if (result.integerValue == 0) {
           
            [self handleListWithModel:array];
        }else{
             [MBProgressHUD showError:resultNote];
        }

    } fail:^(NSError *error) {

    }];
}


-(void)endRefrash

{
    if (self.tableView.mj_header.state == MJRefreshStateRefreshing) {
        [self.tableView.mj_header endRefreshing];
    }
    
    if (self.tableView.mj_footer.state == MJRefreshStateRefreshing) {
        [self.tableView.mj_footer endRefreshing];
    }
}
-(void)handleListWithModel:(NSArray *)result
{
    if (![result isKindOfClass:[NSArray class]]) {
        return;
    }
    
    if (self.pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    if (result.count != kPage_Size) {
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
    }else{
        self.tableView.mj_footer.state = MJRefreshStateIdle;
    }
    
    [self.dataArray addObjectsFromArray:result];
   
   [SSKJ_NoDataView showNoData:self.dataArray.count toView:self.tableView offY:TSScaleW(10) message:@"还没有数据哦~" imge:[UIImage imageNamed:@"nolikesimg"]];
    
    self.pageNum++;
    
    [self endRefrash];
    
    [self.tableView reloadData];
    
}
@end
